package video;

import java.io.*;
import java.util.Vector;

import javax.media.*;
import javax.media.format.VideoFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;


public class videoStream {

	public String ipAddress;
	public DataSink dataSink;
	public Processor processor;
	public videoStream(String ipstring)
	{
		dataSink = null;
		processor = null;
		ipAddress = ipstring;
	}
	
	public void Start()
	{
		System.out.println("Attempting to start video feed");
		//our capture device
		CaptureDeviceInfo videoCDI = null;
		
		//our capture device's information
	    CaptureDeviceInfo cdi;
	    
	    //all available capture devices on the system
		Vector devices;
		
		//all video capture devices on the system
		Vector videoDevices;
		
		//all video formats available for our capture device
		Vector videoFormats;
		
		//our chosen video format. needs to be an array for when we
		//create the processor model. It takes an array so we can have
		//a video format and audio format, but we're only interested in
		//video. so its an array of 1.
		Format[] videoFormat = new Format[1];
		
		//the number of capture devices in the system
		int deviceCount;

		//get the devices we can use
		devices = CaptureDeviceManager.getDeviceList ( null );
		
		//find out how many we have
		deviceCount = devices.size();
		
		//blah
		videoDevices = new Vector();
		videoFormats = new Vector();
		
		//current formats for each device
	    Format[] formats;
	      //loop through the devices
	      for ( int i = 0;  i < deviceCount;  i++ ) {
	    	//get the information for the current device  
	        cdi = (CaptureDeviceInfo) devices.elementAt ( i );
	        //get the formats for the current device
	        formats = cdi.getFormats();
	        //loop through the formats for this device
	        for ( int j=0;  j<formats.length; j++ ) {
	        	//if its a video format we've got a "camera"
	        	if (formats[j] instanceof VideoFormat ) {
	        	//add it to our vector of video devices	
	            videoDevices.addElement(cdi);
	            //add its format to our vector of formats
	            videoFormats.addElement(formats[j]);
	            //break;
	          }
	        }
	      }
	      
	    //set the video device(should be the only one available on our server)
	    cdi =(CaptureDeviceInfo) videoDevices.elementAt(0);
	    System.out.print(cdi);
	    //set up our format (might be more than one, but we'll just use the first)
	    formats = cdi.getFormats();
	    videoFormat[0] = (Format)videoFormats.elementAt(0);

	    //set up the content descriptor
	    ContentDescriptor CONTENT_DESCRIPTOR =
	    new ContentDescriptor(ContentDescriptor.RAW_RTP);
	    
	    try {
	    	//create the processor model
			ProcessorModel pModel = new ProcessorModel(Manager.createDataSource(cdi.getLocator()),videoFormat,CONTENT_DESCRIPTOR);
			//set up the processor from the processor model
			processor = Manager.createRealizedProcessor(pModel);
			//set up the data source <- this is where our video feed is coming from
			DataSource source = processor.getDataOutput();
			//set up the place to send the video to
			//******** needs to be the ip address of the server ****************
			//******** this string needs to be sent to the client ***************
			//******** so it knows where to look for the video feed **************
			//******** should probably use port 8080 to make sure it **************
			//******** works in arch for the presentation.           **************
			MediaLocator dest = new MediaLocator("rtp:/" + ipAddress + ":9060/video");
			
			//set up the datasink <- this actually pushes the video stream out
			//onto the network. we tell it where the video feed is coming from
			//and were it needs to be sent to as rtp packets
			dataSink= null;
			dataSink = Manager.createDataSink(source, dest);

			//open up the datasink
			System.out.println("Opening dataSink");
			dataSink.open();
			//start the datasink
			System.out.println("Starting dataSink");
			dataSink.start();
			//start the processor
			System.out.println("Starting processor");
			processor.start();
			//our video feed should now be streaming to the servers rtp://serverip:8080/video
			System.out.println("Everything Started");
		} catch (NoDataSourceException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (NoProcessorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CannotRealizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoDataSinkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Stop()
	{
		 try {
			dataSink.stop();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 dataSink.close();
		 processor.stop();
		 processor.close();
	}
}
